package cn.tsvtc.shop.controller;


import cn.tsvtc.shop.constant.CookieConstant;
import cn.tsvtc.shop.entity.ResultMessage;
import cn.tsvtc.shop.feign.LoginFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Athena
 * @date: 2020/10/31 11:31
 * @description:
 */
@Controller
@RequestMapping("index")
@Slf4j
public class IndexController {

    private RestTemplate restTemplate;

    private LoginFeign loginFeign;

    @Autowired
    public IndexController(LoginFeign loginFeign,RestTemplate restTemplate){
        this.loginFeign=loginFeign;
        this.restTemplate=restTemplate;
    }





//    @RequestMapping("status.action")
//    @ResponseBody
//    public ResultMessage<String> status(@CookieValue(name = "login_token",required = false) String uuid){
//        log.error("uuid===>"+uuid);
//        ResultMessage<String> status = loginFeign.status(uuid);
//        return status;
//    }

    @RequestMapping("status.action")
    @ResponseBody
    public ResultMessage<String> status(@CookieValue(name = "login_token",required = false) String uuid){
        String url="http://shop-user-service:8001/login/status.action";
        HttpHeaders headers=new HttpHeaders();
        List cookie=new ArrayList();
        cookie.add("login_token=" + uuid);
        headers.put(HttpHeaders.COOKIE,cookie);
        HttpEntity httpEntity=new HttpEntity(headers);
        ResultMessage resultMessage = restTemplate.postForObject(url,httpEntity, ResultMessage.class);
        return resultMessage;
    }


    @RequestMapping("logout.action")
    @ResponseBody
    public ResultMessage<String> logout(@CookieValue(name = "login_token",required = false) String uuid, HttpServletResponse response){
        String url="http://shop-user-service:8001/login/logout.action";
        HttpHeaders headers=new HttpHeaders();
        List cookie=new ArrayList();
        cookie.add("login_token=" + uuid);
        headers.put(HttpHeaders.COOKIE,cookie);
        HttpEntity httpEntity=new HttpEntity(headers);
        ResultMessage resultMessage = restTemplate.postForObject(url,httpEntity, ResultMessage.class);
        Cookie cookie1 = new Cookie(CookieConstant.LOGIN_TOKEN, "");
        //重置cookie
        cookie1.setMaxAge(0);
        cookie1.setPath("/");
        response.addCookie(cookie1);
        return resultMessage;
    }


}
